package ch.fhnw.ams.sudoku;

import java.util.ArrayList;
import java.util.List;

import ch.fhnw.ams.sudoku.SudokuGame.SudokuState;

/**
 * This class represents an observable object. It is designed to be subclassed
 * by {@link SudokuGame}. The {@link SudokuObservable} object can have one or
 * more {@link SudokuObserver}.
 * 
 * @author amsler
 * 
 */
public abstract class SudokuObservable {
	
	private List<SudokuObserver> observers = new ArrayList<SudokuObserver>();
	
	
	
	/**
	 * Adds an observer to the list of observers for this object.
	 * 
	 * @param sudokuObserver
	 *            an observer to be added.
	 */
	public void addObserver(SudokuObserver sudokuObserver) {
		observers.add(sudokuObserver);
	}
	
	/**
	 * Removes an observer from the list
	 * 
	 * @param sudokuObserver
	 *            the observer to be removed
	 */
	public void removeObserver(SudokuObserver sudokuObserver) {
		observers.remove(sudokuObserver);
	}
	
	/**
	 *  Clears the observer list 
	 */
	public void deleteObservers() {
		observers.clear();
	}
	
	/**
	 * Notifies all observers that the {@link SudokuState} has changed.
	 * 
	 * @param oldState
	 *            the state before the change
	 * @param newState
	 *            the state after the change
	 */
	public void notifyObservers(SudokuState oldState, SudokuState newState) {
		for (SudokuObserver observer : observers) {
			observer.stateChanged(oldState, newState);
		}
	}
	
}



